package com.eleadmin.order.biz.impl;

import com.eleadmin.common.system.entity.User;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.DateUtil;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.order.biz.ContainerBillProcessBiz;
import com.eleadmin.order.entity.ContainerBillProcess;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2023-04-03 14:36:45
 * @version ：1.0
 */
@Repository
public class ContainerBillProcessBizImpl extends BaseDao implements ContainerBillProcessBiz {

    @Override
    public JsonResult list(PageResult2<T> page, ContainerBillProcess containerBillProcess, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<ContainerBillProcess> rm = new BeanPropertyRowMapper<ContainerBillProcess>(ContainerBillProcess.class);
        StringBuffer sql = new StringBuffer("select c.id,c.bill_id,c.state");
        sql.append(",c.remark,c.create_time,c.create_user,c.update_time");
		sql.append(",c.update_user");
        sql.append(",suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_container_bill_process c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=c.update_user ");
        sql.append(" where c.deleted=0 and c.bill_id=:bill_id");
        map.put("bill_id", containerBillProcess.getBillId());
        if(Utils.isNotEmpty(containerBillProcess)){
    //			if(Utils.isNotEmpty(containerBillProcess.getName())){
    //				sql.append(" and instr(c.name,:name)>0 ");
    //				map.put("name", containerBillProcess.getName());
    //			}
            if(Utils.isNotEmpty(containerBillProcess.getSort())&&Utils.isNotEmpty(containerBillProcess.getOrder())){
                sql.append(" order by c."+containerBillProcess.getSort()+" "+containerBillProcess.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<ContainerBillProcess> list(ContainerBillProcess containerBillProcess, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<ContainerBillProcess> rm = new BeanPropertyRowMapper<ContainerBillProcess>(ContainerBillProcess.class);
        StringBuffer sql = new StringBuffer("select c.id,c.bill_id,c.state");
        sql.append(",c.remark,c.create_time,c.create_user,c.update_time");
		sql.append(",c.update_user");
        sql.append(",suser.nickname createName ");
        sql.append(" from chf_container_bill_process c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" where c.deleted=0 and c.bill_id=:bill_id");
        map.put("bill_id", containerBillProcess.getBillId());
        if(Utils.isNotEmpty(containerBillProcess)){
            if(Utils.isNotEmpty(containerBillProcess.getSort())&&Utils.isNotEmpty(containerBillProcess.getOrder())){
                sql.append(" order by c."+containerBillProcess.getSort()+" "+containerBillProcess.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public ContainerBillProcess getById(String id) {
        RowMapper<ContainerBillProcess> rm = new BeanPropertyRowMapper<ContainerBillProcess>(ContainerBillProcess.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select c.id,c.bill_id,c.state");
        sql.append(",c.remark,c.create_time,c.create_user,c.update_time");
		sql.append(",c.update_user");
        sql.append(",suser.nickname createName ");
        sql.append(" from chf_container_bill_process c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" where c.deleted=0 and c.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(ContainerBillProcess containerBillProcess, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into chf_container_bill_process (");
        sql.append("bill_id,state,remark,create_user,create_time) values ( ");
        sql.append(":bill_id,:state,:remark,:create_user,:create_time)");
        map.put("bill_id", containerBillProcess.getBillId());
        map.put("state", containerBillProcess.getState());
        map.put("remark", containerBillProcess.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        if (Utils.isNotEmpty(containerBillProcess.getCreateTime())) {
            map.put("create_time", DateUtil.formatDate(new Date(new Date().getTime()+1000)));
        } else {
            map.put("create_time", DateUtil.getCurrentDate());
        }
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(ContainerBillProcess containerBillProcess, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update chf_container_bill_process set ");
        sql.append("bill_id=:bill_id,state=:state,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("bill_id", containerBillProcess.getBillId());
        map.put("state", containerBillProcess.getState());
        map.put("remark", containerBillProcess.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", containerBillProcess.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(id.length()>0){
            for(String id1 : id.split(",")){
                map  = new HashMap<String, String>();
                if(id1==null || id1.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update chf_container_bill_process set deleted=1" +
                    ",update_user=:update_user, update_time=now() "
                    + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}
